package com.springfairy.controller;


import com.springfairy.dto.LoginRequest;
import com.springfairy.dto.RegisterRequest;
import com.springfairy.model.User;
import com.springfairy.service.UserService;
import com.springfairy.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public User register(@RequestBody RegisterRequest req) {
        return userService.register(req.username, req.password, req.email);
    }

    @PostMapping("/login")
    public User login(@RequestBody LoginRequest req,
                      HttpServletResponse response, HttpSession session) {
        return userService.login(req.username, req.password,response,session);
    }

    @GetMapping("/info")
    public User getUserInfo(HttpServletRequest request) {
        return UserUtils.getLoginUser(request);
    }
}
